<?php

namespace App\Kuafu\Activity\Validate;

use App\Http\Validate\AbstractBaseValidate;
use App\Kuafu\Activity\Enum\ActivityEnum;
use App\Kuafu\Coupon\Enum\CouponEnum;
use Illuminate\Http\Request;
use App\Kuafu\Base\Validate\BaseValidate;
use Illuminate\Validation\Rule;

class ActivityValidate extends BaseValidate
{
    public static function getActivityById($data)
    {
        $rules    = [
            'id' => 'required|integer|min:0',
        ];
        $messages = [];

        return self::validateCheck($data, $rules, $messages);
    }

//    public static function getActivityList($data)
//    {
//        $rules    = [
//            'pageSize' => self::commonRules('pageSize'),
//            'pageNum'  => self::commonRules('pageNum'),
//        ];
//        $messages = [];
//
//        return self::validateCheck($data, $rules, $messages);
//    }

    public static function addActivity($data)
    {
        $rules    = [
            'type'               => AbstractBaseValidate::intInValidateBuild(true, ActivityEnum::TYPES),
            'name'               => 'required|string|min:1|max:50',
            'startAt'            => 'required_if:timeType,' . ActivityEnum::TIME_TYPE_SHORT . '|string|min:19|max:19',
            'endAt'              => 'required_if:timeType,' . ActivityEnum::TIME_TYPE_SHORT . '|string|min:19|max:19',
            'timeType'           => 'required|int|in:' . implode(',', array_keys(ActivityEnum::TIME_TYPES)),
            'shopType'           => 'required|int|in:' . implode(',', array_keys(ActivityEnum::SHOP_TYPES)),
            'shopIds'            => 'required_if:shopType,' . ActivityEnum::SHOP_TYPE_SELECTION . '|array',
            'coupons'            => 'required|array',
            'coupons.*.couponId' => 'required|int|min:1',
            'coupons.*.num'      => 'required|int|min:1',
            'description'        => 'sometimes|string|max:200',
            'limitJoinNum'       => 'required|int|min:0'
        ];
        $messages = [];
        if (isset($data["type"]) && $data["type"] == ActivityEnum::TYPE_START_BUSINESS_ACTIVITY) {
            $rules = array_merge($rules, [
                'shopType'           => 'required|int|in:' . ActivityEnum::SHOP_TYPE_SELECTION,
                'shopIds'            => 'required|array|min:1|max:1',
                'shopIds.*'          => 'required|int|min:1',
                'coupons'            => 'required|array|min:1',
                'coupons.*.couponId' => Rule::exists("coupons", "id")->whereIn("type", [CouponEnum::COUPON_TYPE_VOUCHERS,CouponEnum::COUPON_TYPE_EXCHANGE,CouponEnum::COUPON_TYPE_DISCOUNT]),
                'coupons.*.num'      => 'required|int|min:1',
                'isAttendEd'         => 'required|int',
                'pushText'           => '',
                'miniCardImage'      => '',
            ]);
        }
        return self::validateCheck($data, $rules, $messages);
    }

    public static function setActivity($data)
    {
        $rules    = [
            'id' => 'required|integer|min:0',
        ];
        $messages = [];

        return self::validateCheck($data, $rules, $messages);
    }

    public static function getActivityPage($data)
    {
        $rules    = [
            'pageSize' => 'required|integer|min:0',
            'pageNum'  => self::commonRules('pageNum'),
        ];
        $messages = [];

        return self::validateCheck($data, $rules, $messages);
    }

    public static function delActivity($data)
    {
        $rules    = [
            'id' => 'required|integer|min:0',
        ];
        $messages = [];

        return self::validateCheck($data, $rules, $messages);
    }

    public static function patchActivityStatus($data)
    {
        $rules    = [
            'id'     => 'required|integer|min:0',
            'status' => 'required|integer|between:0,1',
        ];
        $messages = [];

        return self::validateCheck($data, $rules, $messages);
    }
}
